var CryptoJS = require('./crypto-js')
var base64 = require('./js-base64').Base64
const baseUrl = 'http://api.51cjml.com/';

const uploadImg = 'http://yc.tengshemall.com/index.php/index.php/Api/uploadfile';

const key ='KmQzOjvS6XB6wxpN';
// const key ='W7NRODQir2sX8nis';

// const date ="date: Tue, 03 Apr 2018 02:57:20 GMT";
// console.log(date)
// let aaar = CryptoJS.HmacSHA1(date,key);
// let aaar = CryptoJS.HmacSHA256(date,key);
// console.log(Base64.encode(aaar))
// let sign = CryptoJS.enc.Base64.stringify(aaar);
// let sign ="KkIedIw/uYrQ5BNOUfKHGzYL/5Q=";
// let sign =Base64.encode(aaar);
const imgUrl = 'http://api.51cjml.com';
const http = ({
	url = '',
	param = {},
	sign='',
	time='',
	...other
} = {}) => {
	uni.showLoading({
		title: '请求中，请耐心等待..'
	});
	let timeStart = Date.now();
	return new Promise((resolve, reject) => {
		uni.request({
			url: getUrl(url),
			data: param,
			header: {
				'content-type': 'application/json' ,// 默认值 ,另一种是
				// "content-type": "application/x-www-form-urlencoded",
				 'authorization':'hmac username="jdyc",algorithm="hmac-sha1",headers="date",signature="'+sign+'"',
				 'date':time
				// "content-type": "multipart/form-data"
				
			},
			...other,
			complete: (res) => {
				uni.hideLoading();
				console.log(`耗时${Date.now() - timeStart}`);
				// console.log(res,"状态码")
				console.log(res.statusCode,"状态码")
				if (res.statusCode >= 200 && res.statusCode < 300) {
					resolve(res.data)
				} else {
					reject(res)
				}
			}
		})
	})
}

const getUrl = (url) => {
	if (url.indexOf('://') == -1) {
		url = baseUrl + url;
	}
	return url
}

// get方法
const _get = (url, param = {}) => {
	return http({
		url,
		param
	})
}
//获取token
const _post = (url, param = {}) => {
	let date ="date: "+new Date().toUTCString();
	let aaar = CryptoJS.HmacSHA1(date,key);
	let sign = CryptoJS.enc.Base64.stringify(aaar);
	let time = new Date().toUTCString();
	console.log(sign,time)
	// param['token'] = uni.getStorageSync('token')
	// param['store_id'] = uni.getStorageSync('store_id')
	return http({
		url,
		param,
		sign,
		time,
		method: 'post'
	})
	// }
}

const _put = (url, param = {}) => {
	return http({
		url,
		param,
		method: 'put'
	})
}

const _delete = (url, param = {}) => {
	return http({
		url,
		param,
		method: 'put'
	})
}
module.exports = {
	baseUrl,
	_get,
	_post,
	_put,
	_delete,
	uploadImg,
	imgUrl,
	// key
}
